package com.airnewslinkosky.ticket.util;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;

import com.airnewslinkosky.ticket.R;

public class NetIOUtils {

	private static final String LOG_TAG = "NetIOUtils";
	public static Uri PREFERRED_APN_URI = Uri.parse("content://telephony/carriers/preferapn");
		
	public static boolean isNetworkAvailable(Activity mActivity) {  
	    Context context = mActivity.getApplicationContext(); 
	    ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 
	    if (connectivity == null) {     
	      return false; 
	    } else {   
	        NetworkInfo[] info = connectivity.getAllNetworkInfo();     
	        if (info != null) {         
	            for (int i = 0; i < info.length; i++) {            
	                if (info[i].getState() == NetworkInfo.State.CONNECTED) {               
	                    return true;  
	                }         
	            }      
	        }  
	    }    
	    return false; 
	}	
	
	/**
	 * 测网络是否存
	 */
	public static void HttpTest(final Activity mActivity) {
		if (!isNetworkAvailable(mActivity)) {
	        AlertDialog alert = new AlertDialog.Builder(mActivity)
	        .setCancelable(false)
            .setIcon(R.drawable.alert_dialog_icon)
            .setTitle(R.string.alert_dialog_error)
            .setMessage(R.string.alert_dialog_net_fail)
            .setPositiveButton(R.string.alert_dialog_yes, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {   
                	mActivity.finish();;   
                }
            })
            .create();
			alert.show();
		}
	}
}
